// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ScriptureChooser.java

package presenter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

// Referenced classes of package presenter:
//            Bible, BibleBook, SongFile

public class ScriptureChooser extends JPanel
    implements ActionListener
{

    public ScriptureChooser(Bible bible)
    {
        update = false;
        book = new JComboBox(bible.allBooks);
        add(book);
        book.addActionListener(this);
        change = new JButton("Update Scripture");
        change.addActionListener(this);
        setStartChapter(bible.getBook("Genesis"));
        rebuildUI();
        this.bible = bible;
        setVisible(true);
    }

    public boolean isUpdated()
    {
        if(update)
        {
            update = false;
            return true;
        } else
        {
            return false;
        }
    }

    public void setSong(SongFile song)
    {
        this.song = song;
    }

    private void setStartChapter(BibleBook bb)
    {
        String choices[] = new String[bb.getNumberOfChapters()];
        for(int i = 0; i < choices.length; i++)
            choices[i] = (new StringBuilder()).append("").append(i + 1).toString();

        startChapter = new JComboBox(choices);
        startChapter.addActionListener(this);
        setStartVerse(bb, 0);
        setEndChapter(bb, 0);
        setEndVerse(bb, 0, 0);
        rebuildUI();
    }

    public void setStartVerse(BibleBook bb, int chapter)
    {
        String choices[] = new String[bb.getNumberOfVerses(chapter)];
        for(int i = 0; i < choices.length; i++)
            choices[i] = (new StringBuilder()).append("").append(i + 1).toString();

        startVerse = new JComboBox(choices);
        startVerse.addActionListener(this);
    }

    public void setEndChapter(BibleBook bb, int start)
    {
        String choices[] = new String[bb.getNumberOfChapters() - start];
        for(int i = 0; i < choices.length; i++)
            choices[i] = (new StringBuilder()).append("").append(i + 1 + start).toString();

        endChapter = new JComboBox(choices);
        endChapter.addActionListener(this);
    }

    public void setEndVerse(BibleBook bb, int chapter, int start)
    {
        String choices[] = new String[bb.getNumberOfVerses(chapter) - start];
        for(int i = 0; i < choices.length; i++)
            choices[i] = (new StringBuilder()).append("").append(i + 1 + start).toString();

        endVerse = new JComboBox(choices);
    }

    public void actionPerformed(ActionEvent e)
    {
        BibleBook bb = bible.getBook((String)book.getSelectedItem());
        if(e.getSource() == book)
            setStartChapter(bible.getBook((String)book.getSelectedItem()));
        else
        if(e.getSource() == startChapter)
        {
            setStartVerse(bb, startChapter.getSelectedIndex());
            setEndChapter(bb, startChapter.getSelectedIndex());
            setEndVerse(bb, startChapter.getSelectedIndex(), 0);
        } else
        if(e.getSource() == startVerse)
        {
            if(endChapter.getSelectedIndex() == 0)
                setEndVerse(bb, startChapter.getSelectedIndex(), startVerse.getSelectedIndex());
        } else
        if(e.getSource() == endChapter)
        {
            if(endChapter.getSelectedIndex() > 0)
                setEndVerse(bb, startChapter.getSelectedIndex() + endChapter.getSelectedIndex(), 0);
            else
                setEndVerse(bb, startChapter.getSelectedIndex(), 0);
        } else
        if(e.getSource() == change)
        {
            int end = endVerse.getSelectedIndex();
            if(endChapter.getSelectedIndex() == 0)
                end += startVerse.getSelectedIndex();
            song.setVerses(bb.getVerses(startChapter.getSelectedIndex(), startVerse.getSelectedIndex(), startChapter.getSelectedIndex() + endChapter.getSelectedIndex(), end));
        }
        rebuildUI();
    }

    public void rebuildUI()
    {
        java.awt.Component component[] = getComponents();
        for(int i = 0; i < component.length; i++)
            remove(component[i]);

        add(book);
        add(startChapter);
        add(new JLabel(":"));
        add(startVerse);
        add(new JLabel("-"));
        add(endChapter);
        add(new JLabel(":"));
        add(endVerse);
        add(change);
        updateUI();
        repaint();
    }

    private boolean update;
    private SongFile song;
    JButton change;
    JComboBox book;
    JComboBox startChapter;
    JComboBox startVerse;
    JComboBox endChapter;
    JComboBox endVerse;
    Bible bible;
}
